const point = require('../models/point');
const question = require('../models/question');

var express = require('express');
var router = express.Router()

var formidable = require('formidable')

router.post('/', (req, res, next) => {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
        if(err) {
            res.json({'state': false, 'msg': 'There is somethin wrong!'});
        }else {
            if(fields.userGroup !== "0"){
                if(fields.question_id) {
                    question.find({
                        _id: fields.question_id
                    }, (err, docs) => {
                        if(err) {
                            res.json({'state': false, 'msg': err})
                        }else {
                            let tmpquestion = {}
                            tmpquestion.questionId = docs[0]._id;
                            tmpquestion.pointId = docs[0].point_id;
                            tmpquestion.items = docs[0].items;
                            tmpquestion.answer = docs[0].answer;
                            tmpquestion.content = docs[0].question;
                            tmpquestion.type = docs[0].type;
                            tmpquestion.illustrationContent = docs[0].illustrationContent;
                            tmpquestion.bloom = docs[0].bloom;
                            res.json({
                                'state': true,
                                question: tmpquestion
                            })
                        }
                    })
                }else {
                    point.find({
                        _id: fields.point_id
                    }, (err, docs) => {
                        if(err) {
                            res.json({'state': false, 'msg': err})
                        }else {
                            let tmpquestion = {}
                            tmpquestion.questionId = '';
                            tmpquestion.pointId = docs[0]._id;
                            tmpquestion.items = docs[0].practice.items;
                            tmpquestion.answer = docs[0].practice.answer;
                            tmpquestion.content = docs[0].practice.question_editor_Content;
                            tmpquestion.type = docs[0].practice.type;
                            tmpquestion.illustrationContent = docs[0].practice.illustrationContent;
                            tmpquestion.bloom = docs[0].bloom;
                            res.json({
                                'state': true,
                                question: tmpquestion
                            })
                        }
                    })
                }
            }else if(fields.userGroup === "0"){
                if(fields.question_id) {
                    point.find({
                        _id: fields.point_id,
                    }, (err, p) => {
                        console.log(p[0]);
                        question.find({
                            _id: fields.question_id
                        }, (err, docs) => {
                            if(err) {
                                res.json({'state': false, 'msg': err})
                            }else {
                                let tmpquestion = {}
                                tmpquestion.questionId = docs[0]._id;
                                tmpquestion.pointId = docs[0].point_id;
                                tmpquestion.title = p[0].title;
                                tmpquestion.Pointcontent = p[0].content;
                                tmpquestion.items = docs[0].items;
                                tmpquestion.content = docs[0].question;
                                tmpquestion.type = docs[0].type;
                                tmpquestion.bloom = docs[0].bloom;
                                res.json({
                                    'state': true,
                                    question: tmpquestion
                                })
                            }
                        })
                    })
                }else {
                    point.find({
                        _id: fields.point_id
                    }, (err, docs) => {
                        if(err) {
                            res.json({'state': false, 'msg': err})
                        }else {
                            console.log(docs[0])
                            let tmpquestion = {}
                            tmpquestion.questionId = '';
                            tmpquestion.pointId = docs[0]._id;
                            tmpquestion.title = docs[0].title;
                            tmpquestion.pointContent = docs[0].content;
                            tmpquestion.items = docs[0].practice.items;
                            tmpquestion.content = docs[0].practice.question_editor_Content;
                            tmpquestion.type = docs[0].practice.type;
                            tmpquestion.bloom = docs[0].bloom;
                            res.json({
                                'state': true,
                                question: tmpquestion
                            })
                        }
                    })
                }
            }else{
                res.json({'state': false, 'msg': 'wrong userGroup!'})
            }
        }
    });
});

module.exports = router;